<!--
 * @Author: Rain
 * @Date: 2023/10/11 10:08:34
 * @LastEditTime: 2023/10/22 16:52:33
 * @Description: Do Something
-->
<template>
  <div class="app-container w-full h-full overflow-hidden">
    <div class="head-container w-full h-full overflow-hidden flex flex-col">
      <div class="w-full flex items-center justify-end pb-10 search">
        <!-- <label class="el-form-item-label">题库名称</label>
        <el-input
          v-model="query.databaseName"
          clearable
          placeholder="题库名称"
          style="width: 185px"
          class="filter-item mr-10"
          @keyup.enter.native="crud.toQuery"
        /> -->
        <label class="el-form-item-label">测评单元</label>
        <el-select
          v-model="query.atmrExamTypeId"
          placeholder="请选择"
          filterable
          clearable
          class="mr-10"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in typeList"
            :key="item.id"
            :label="item.atmrName"
            :value="item.id"
          />
        </el-select>
        <rrOperation :crud="crud" />
        <crudOperation :permission="permission" />
      </div>
      <div class="w-full flex-1 overflow-hidden">
        <el-table
          ref="table"
          v-loading="crud.loading"
          :data="crud.data"
          size="small"
          style="width: 100%"
          height="100%"
          stripe
          @selection-change="crud.selectionChangeHandler"
        >
          <el-table-column type="selection" width="55" />
          <el-table-column prop="databaseName" label="题库名称" />
          <el-table-column prop="atmrExamTypeId" label="测评单元">
            <template slot-scope="{ row }">
              {{ row.atmrExamTypeId | getTypeName(typeList) }}
            </template>
          </el-table-column>
          <el-table-column label="试题" width="55">
            <template slot-scope="{ row }">
              <i class="el-icon-search cursor-pointer" @click="viewAction(row)" />
            </template>
          </el-table-column>
          <el-table-column
            v-if="checkPer(['admin', 'examDatabase:edit', 'examDatabase:del'])"
            label="操作"
            width="230px"
            align="center"
          >
            <template slot-scope="scope">
              <div class="w-full flex items-center justify-center">
                <el-upload
                  class="mr-2"
                  action="/api/examInfo/upload"
                  :headers="{ Authorization: token }"
                  :data="{ databaseId: scope.row.databaseId }"
                  accept=".xlsx"
                  :on-success="handleSuccess"
                  :show-file-list="false"
                  :on-error="handleError"
                >
                  <el-button
                    type="primary"
                    size="mini"
                    icon="el-icon-upload"
                  >导入试题</el-button>
                </el-upload>
                <udOperation :data="scope.row" :permission="permission" />
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!--分页组件-->
      <pagination />
    </div>
    <el-dialog
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible.sync="crud.status.cu > 0"
      :title="crud.status.title"
      width="500px"
    >
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
        <el-form-item
          label="题库名称"
          prop="databaseName"
          :rules="[{ required: true, message: '题库名称不能为空', trigger: 'blur' }]"
        >
          <el-input v-model="form.databaseName" style="width: 370px" />
        </el-form-item>
        <el-form-item
          label="测评单元"
          prop="atmrExamTypeId"
          :rules="[{ required: true, message: '测评单元不能为空', trigger: 'blur' }]"
        >
          <el-select
            v-model="form.atmrExamTypeId"
            placeholder="请选择"
            filterable
            class="w-full"
          >
            <el-option
              v-for="item in typeList"
              :key="item.id"
              :label="item.atmrName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="default" @click="crud.cancelCU">取消</el-button>
        <el-button
          :loading="crud.status.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import crudExamDatabase from '@/api/examDatabase'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { initData } from '@/api/data'
import { getToken } from '@/utils/auth'

const defaultForm = {
  databaseId: null,
  databaseName: null,
  atmrExamTypeId: null,
  createTime: null,
  createby: null,
  updateTime: null,
  updateby: null
}
export default {
  name: 'ExamDatabase',
  filters: {
    getTypeName(value, typeList) {
      const item = typeList.find((item) => item.id === value)
      return item && item.atmrName
    }
  },
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({
      title: '题库',
      url: 'api/examDatabase',
      idField: 'databaseId',
      sort: 'databaseId,desc',
      crudMethod: { ...crudExamDatabase }
    })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'examDatabase:add'],
        edit: ['admin', 'examDatabase:edit'],
        del: ['admin', 'examDatabase:del']
      },
      rules: {
        databaseName: [{ required: true, message: '题库名称不能为空', trigger: 'blur' }],
        atmrExamTypeId: [
          { required: true, message: '测评单元不能为空', trigger: 'blur' }
        ]
      },
      typeList: [],
      questionList: [],
      queryTypeOptions: [
        { key: 'databaseName', display_name: '题库名称' },
        { key: 'atmrExamTypeId', display_name: '测试单元' }
      ]
    }
  },
  computed: {
    token() {
      return getToken()
    }
  },
  mounted() {
    initData('api/atmr', { size: 1000000, page: 0 }).then((res) => {
      const { content } = res
      this.typeList = content
    })
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    viewAction(row) {
      const { databaseId } = row
      this.$router.push({ name: 'examInfo', params: { databaseId }})
      // initData('api/examInfo', { examDatabaseId: databaseId }).then((res) => {
      //   const { content } = res
      //   this.questionList = content
      // })
    },
    handleSuccess() {
      this.$message({
        type: 'success',
        message: '导入成功'
      })
    },
    handleError() {
      this.$message({
        type: 'error',
        message: '导入失败,请重试!'
      })
    }
  }
}
</script>

<style scoped></style>
